/*
 * @(#)CommentView.java	1.13 04/03/05
 *
 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  guiparser;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.text.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;

/**
 * CommentView subclasses HiddenTagView to contain a JTextArea showing
 * a comment. When the textarea is edited the comment is
 * reset. As this inherits from EditableView if the JTextComponent is
 * not editable, the textarea will not be visible.
 *
 * @author  Scott Violet
 * @version 1.13, 03/05/04
 */
class CommentView extends HiddenTagView {
    CommentView(Element e) {
	super(e);
    }

    protected Component createComponent() {
        Container host = getContainer();
        if (host != null && !((JTextComponent)host).isEditable()) {
            return null;
        }
	JTextArea ta = new JTextArea(getRepresentedText());
	Document doc = getDocument();
	Font font;
	if (doc instanceof StyledDocument) {
	    font = ((StyledDocument)doc).getFont(getAttributes());
	    ta.setFont(font);
	}
	else {
	    font = ta.getFont();
	}
	updateYAlign(font);
	ta.setBorder(CBorder);
	ta.getDocument().addDocumentListener(this);
	ta.setFocusable(isVisible());
	return ta;
    }

    void resetBorder() {
    }

    /**
     * This is subclassed to put the text on the Comment attribute of
     * the Element's AttributeSet.
     */
    void _updateModelFromText() {
	JTextComponent textC = getTextComponent();
	Document doc = getDocument();
	if (textC != null && doc != null) {
	    String text = textC.getText();
	    SimpleAttributeSet sas = new SimpleAttributeSet();
	    isSettingAttributes = true;
	    try {
		sas.addAttribute(HTML.Attribute.COMMENT, text);
		((StyledDocument)doc).setCharacterAttributes
		    (getStartOffset(), getEndOffset() -
		     getStartOffset(), sas, false);
	    }
	    finally {
		isSettingAttributes = false;
	    }
	}
    }

    JTextComponent getTextComponent() {
	return (JTextComponent)getComponent();
    }

    String getRepresentedText() {
	AttributeSet as = getElement().getAttributes();
	if (as != null) {
	    Object comment = as.getAttribute(HTML.Attribute.COMMENT);
	    if (comment instanceof String) {
		return (String)comment;
	    }
	}
	return "";
    }

    static final Border CBorder = new CommentBorder();
    static final int commentPadding = 3;
    static final int commentPaddingD = commentPadding * 3;

    static class CommentBorder extends LineBorder {
	CommentBorder() {
	    super(Color.black, 1);
	}

	public void paintBorder(Component c, Graphics g, int x, int y,
				int width, int height) {
	    super.paintBorder(c, g, x + commentPadding, y,
			      width - commentPaddingD, height);
	}

	public Insets getBorderInsets(Component c) {
	    Insets retI = super.getBorderInsets(c);

	    retI.left += commentPadding;
	    retI.right += commentPadding;
	    return retI;
	}

	public boolean isBorderOpaque() {
	    return false;
	}
    } // End of class CommentView.CommentBorder
} // End of CommentView
